package com.heima.leadnews.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.leadnews.admin.mapper.AdChannelMapper;
import com.heima.leadnews.admin.service.AdChannelService;
import com.heima.model.admin.dto.ChannelDto;
import com.heima.model.admin.pojo.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


/**
 * @author zhuangzhiwen
 */
@Service
@Transactional
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper,AdChannel> implements AdChannelService {
    /**
     * 分页列表查询
     * @param dto
     * @return
     */

    @Override
    public ResponseResult listChannel(ChannelDto dto) {
        //1,判断数据是否为空
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //默认值传参
        dto.checkParam();

        //2,构建分页查询器
        IPage<AdChannel> page=new Page<>(dto.getPage(),dto.getSize());

        //3,构建数据库查询
        LambdaQueryWrapper<AdChannel> channelLambdaQw=new LambdaQueryWrapper<>();

        if(StringUtils.isNoneBlank(dto.getName())){
            //3.1判断是否用关键词查询
            channelLambdaQw.like(AdChannel::getName,dto.getName());
        }

        //4,分页查询
        page(page,channelLambdaQw);

        //5,创建分页返回值
        ResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        result.setData(page.getRecords());

        //6，返回数据
        return result;
    }

    /**
     * 频道新增
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1,判断数据是否存在
        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2，封装基本数据
        adChannel.setCreatedTime(new Date());
        adChannel.setIsDefault(true);

        //3,添加到数据库
        save(adChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 频道修改及状态信息修改
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1，判断数据是否存在
        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2，判断数据是否存在
        AdChannel channel = getById(adChannel.getId());
        if(channel==null){
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3,设置默认值
        channel.setCreatedTime(new Date());

        //4,更新状态
//        Boolean status = channel.getStatus();
//        if(status==true){
//            channel.setStatus(false);
//        }
//        if(status==false){
//            channel.setStatus(true);
//        }

        //4,修改数据
        updateById(adChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据id删除频道信息
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {
        //1，判断id是否为空
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2,判断id是否正在使用
        AdChannel channel = getById(id);

        //2.1判断数据是否存在
        if(channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }

        //判断状态
        if(channel.getStatus()==true){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST,"频道有效不能删除");
        }
        //3,否则，删除id
        removeById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}


